import Mock from 'mockjs'
import store from '@/store'
import Tools from '@/plugins/tools'

const pageSize = store.state.pageSize
let maxId = 98

const extraData = Mock.mock({
  // 这里必须提前加入Mock.mock解析，不然下面的extraData.extra.reverse()会无效
  [`extra|${maxId}`]: [
    {
      art_id: '@increment',
      art_pubdate: '@now',
      art_title: '@ctitle(10, 20)',
      artsort_id: '@int(1,7)',
      art_source: '@cword(3, 10)',
      art_order: '@int(0,1000)',
      art_gourl: 'http://@domain',
      art_simg: [
        {
          name: '1',
          url:
            ''
        }
      ],
      art_content: '@cparagraph'
    }
  ]
})

extraData.extra.reverse()

let dataListAll = {
  code: 0,
  msg: '操作成功',
  pageSize: pageSize,
  total: maxId
}

Mock.mock(/\/Article\/listAll/, 'get', options => {
  const p = Tools.getParam('p', options.url)
  const keyword = Tools.getParam('keyword', options.url)
  const artsortId = parseInt(Tools.getParam('artsort_id', options.url))
  const orderType = Tools.getParam('orderType', options.url)
  let tempExtra = extraData.extra
  if (orderType === 'setorder') {
    tempExtra.sort((m, n) => {
      if (m.art_order < n.art_order) {
        return 1
      } else if (m.art_order === n.art_order) {
        return m.art_id < n.art_id ? 1 : -1
      }
      return -1
    })
  } else {
    tempExtra.sort((m, n) => {
      return m.art_id < n.art_id ? 1 : -1
    })
  }
  dataListAll.total = tempExtra.length
  if (artsortId !== 0 && tempExtra.length > 0) {
    tempExtra = tempExtra.filter(item => item.artsort_id === artsortId)
    dataListAll.total = tempExtra.length
  }
  if (keyword !== '' && tempExtra.length > 0) {
    tempExtra = tempExtra.filter(
      item => item.art_title.toLowerCase().indexOf(keyword.toLowerCase()) !== -1
    )
    dataListAll.total = tempExtra.length
  }
  let pExtraData =
    tempExtra.length > 0
      ? tempExtra.slice(pageSize * (p - 1), pageSize * p)
      : tempExtra
  dataListAll.extra = pExtraData
  return dataListAll
})

const dataSuccess = {
  code: 0,
  msg: '操作成功'
}

Mock.mock(/\/Article\/delSelection/, 'post', options => {
  const result = JSON.parse(options.body)
  const ids = result.ids.valueOf()
  extraData.extra = extraData.extra.filter(item => {
    if (ids.includes(item.art_id.toString())) {
      dataListAll.total--
      return false
    }
    return true
  })
  dataSuccess.total = dataListAll.total
  return dataSuccess
})

const dataError = {
  code: 1,
  msg: '文章不存在或者已删除'
}

Mock.mock(/\/Article\/del/, 'post', options => {
  const result = JSON.parse(options.body)
  const id = result.id
  let isExists = false
  extraData.extra = extraData.extra.filter(item => {
    if (item.art_id === id) {
      isExists = true
    }
    return item.art_id !== id
  })
  if (!isExists) {
    return dataError
  }
  dataListAll.total--
  dataSuccess.total = dataListAll.total
  return dataSuccess
})

Mock.mock(/\/Article\/add/, 'post', options => {
  const result = JSON.parse(options.body)
  result.art_id = ++maxId
  extraData.extra.unshift(result)
  dataListAll.total++
  return dataSuccess
})

Mock.mock(/\/Article\/edit/, 'post', options => {
  const result = JSON.parse(options.body)
  let isExists = false
  extraData.extra = extraData.extra.map(item => {
    if (item.art_id === result.art_id) {
      isExists = true
      Object.assign(item, result)
    }
    return item
  })
  if (!isExists) {
    return dataError
  }
  return dataSuccess
})

Mock.mock(/\/Article\/detail/, 'get', options => {
  const id = parseInt(Tools.getParam('id', options.url))
  let tempData
  extraData.extra.every(item => {
    if (item.art_id === id) {
      tempData = {}
      tempData.code = 0
      tempData.msg = '操作成功'
      tempData.extra = item
      return false
    }
    return true
  })
  return tempData || dataError
})

const dataArtsortError = {
  code: 1,
  msg: '分类下有文章，请先删除文章'
}

// 为了检测方便，模拟环境下将分类下文章检测放到Article中，实际开发中建议放到Artsort中
Mock.mock(/\/Article\/checkArtsort/, 'get', options => {
  const id = parseInt(Tools.getParam('id', options.url))
  let isExists = extraData.extra.some(item => item.artsort_id === id)
  return isExists ? dataArtsortError : dataSuccess
})
